package leetcode.sword;

public class Solution62 {

    public static void main(String[] args) {
        Solution62 s = new Solution62();
        System.out.println(s.lastRemaining(10,17));
    }



    public int lastRemaining(int n, int m) {
        LinkedList head = new LinkedList(-1,null);
        LinkedList pre = head;
        for (int i = 0; i < n; i++) {
            pre.next = new LinkedList(i,null);
            pre = pre.next;
        }
        pre.next = head.next; //构成环
        //开始删除
        LinkedList cur = pre.next;//把指针指向0
        while(cur.next!=cur){
            //把指针往后移m-1次
            for (int i = 1; i < m; i++){
                pre = pre.next;
                cur = cur.next;
            }
            //删除cur
            pre.next = cur.next;
            cur = pre.next;
        }
        return cur.val;
    }
    class LinkedList{
        int val;
        LinkedList next;
        LinkedList(int val, LinkedList next){
            this.val = val;
            this.next = next;
        }
    }


}
